package com.example.mister5aside;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.TableRow;
import android.widget.TextView;

public class TeamActivity extends Activity {

	int rowSelected;
	TableRow table_row_selected;
	TextView playerSelected;
	TextView minSelected;
	TextView goalSelected;
	static CharSequence[] players;
	static int[] positions = new int[10];
	static long[] mills;
	static int[] goals;
	
	
	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
	    super.onCreate(savedInstanceState);
	    setContentView(R.layout.activity_team);
	    players = new CharSequence[10];
	    rowSelected=-1;
	    Bundle bundle = getIntent().getExtras();
	    players=bundle.getCharSequenceArray("players");
	    positions=bundle.getIntArray("positions");
	    mills=bundle.getLongArray("mills");
	    goals=bundle.getIntArray("goals");
	    loadPlayers();
	}
	
    @Override
    protected void onStop() {
        super.onStop();
        // The activity is no longer visible (it is now "stopped")
   
    }
    
    @Override
    protected void onPause() {
        super.onPause();
        // The activity is no longer visible (it is now "stopped")
  
    }

    public void back(View v){
    	Intent returnIntent = new Intent();
        returnIntent.putExtra("positions", positions);
        setResult(RESULT_OK,returnIntent);  
        finish();
    }
    
    public void loadPlayers(){
    	playerSelected = (TextView) findViewById(R.id.player0);
    	playerSelected.setText(players[positions[0]]);
    	playerSelected = (TextView) findViewById(R.id.player1);
    	playerSelected.setText(players[positions[1]]);
    	playerSelected = (TextView) findViewById(R.id.player2);
    	playerSelected.setText(players[positions[2]]);
    	playerSelected = (TextView) findViewById(R.id.player3);
    	playerSelected.setText(players[positions[3]]);
    	playerSelected = (TextView) findViewById(R.id.player4);
    	playerSelected.setText(players[positions[4]]);
    	playerSelected = (TextView) findViewById(R.id.player5);
    	playerSelected.setText(players[positions[5]]);
    	playerSelected = (TextView) findViewById(R.id.player6);
    	playerSelected.setText(players[positions[6]]);
    	playerSelected = (TextView) findViewById(R.id.player7);
    	playerSelected.setText(players[positions[7]]);
    	playerSelected = (TextView) findViewById(R.id.player8);
    	playerSelected.setText(players[positions[8]]);
    	
    	minSelected = (TextView) findViewById(R.id.min0);
    	minSelected.setText(String.valueOf(mills[positions[0]]/60000)+":"+String.valueOf((mills[positions[0]]/1000)%60));
    	minSelected = (TextView) findViewById(R.id.min1);
    	minSelected.setText(String.valueOf(mills[positions[1]]/60000)+":"+String.valueOf((mills[positions[1]]/1000)%60));
    	minSelected = (TextView) findViewById(R.id.min2);
    	minSelected.setText(String.valueOf(mills[positions[2]]/60000)+":"+String.valueOf((mills[positions[2]]/1000)%60));
    	minSelected = (TextView) findViewById(R.id.min3);
    	minSelected.setText(String.valueOf(mills[positions[3]]/60000)+":"+String.valueOf((mills[positions[3]]/1000)%60));
    	minSelected = (TextView) findViewById(R.id.min4);
    	minSelected.setText(String.valueOf(mills[positions[4]]/60000)+":"+String.valueOf((mills[positions[4]]/1000)%60));
    	minSelected = (TextView) findViewById(R.id.min5);
    	minSelected.setText(String.valueOf(mills[positions[5]]/60000)+":"+String.valueOf((mills[positions[5]]/1000)%60));
    	minSelected = (TextView) findViewById(R.id.min6);
    	minSelected.setText(String.valueOf(mills[positions[6]]/60000)+":"+String.valueOf((mills[positions[6]]/1000)%60));
    	minSelected = (TextView) findViewById(R.id.min7);
    	minSelected.setText(String.valueOf(mills[positions[7]]/60000)+":"+String.valueOf((mills[positions[7]]/1000)%60));
    	minSelected = (TextView) findViewById(R.id.min8);
    	minSelected.setText(String.valueOf(mills[positions[8]]/60000)+":"+String.valueOf((mills[positions[8]]/1000)%60));
    	
    	goalSelected = (TextView) findViewById(R.id.goal0);
    	goalSelected.setText(String.valueOf(goals[positions[0]]));
    	goalSelected = (TextView) findViewById(R.id.goal1);
    	goalSelected.setText(String.valueOf(goals[positions[1]]));
    	goalSelected = (TextView) findViewById(R.id.goal2);
    	goalSelected.setText(String.valueOf(goals[positions[2]]));
    	goalSelected = (TextView) findViewById(R.id.goal3);
    	goalSelected.setText(String.valueOf(goals[positions[3]]));
    	goalSelected = (TextView) findViewById(R.id.goal4);
    	goalSelected.setText(String.valueOf(goals[positions[4]]));
    	goalSelected = (TextView) findViewById(R.id.goal5);
    	goalSelected.setText(String.valueOf(goals[positions[5]]));
    	goalSelected = (TextView) findViewById(R.id.goal6);
    	goalSelected.setText(String.valueOf(goals[positions[6]]));
    	goalSelected = (TextView) findViewById(R.id.goal7);
    	goalSelected.setText(String.valueOf(goals[positions[7]]));
    	goalSelected = (TextView) findViewById(R.id.goal8);
    	goalSelected.setText(String.valueOf(goals[positions[8]]));

    }
	
    public void row0(View v)
    {
    	switch(rowSelected){
    	case -1:
    		rowSelected = 0;
    		table_row_selected = (TableRow) findViewById(R.id.Row0);
    		table_row_selected.setBackgroundColor(getResources().getColor(R.color.blue));
    		break;
    	case 0:
    		rowSelected = -1;
    		table_row_selected = (TableRow) findViewById(R.id.Row0);
    		table_row_selected.setBackgroundColor(getResources().getColor(R.color.green));
    		break;
    	case 1:
    	case 2:
    	case 3:
    	case 4:
    		break;
    	default:
    		int tmp;
    		tmp = positions[0];
    		positions[0]=positions[rowSelected];
    		positions[rowSelected]=tmp;
    		rowSelected = -1;
    		table_row_selected.setBackgroundColor(getResources().getColor(R.color.red));
    		table_row_selected = (TableRow) findViewById(R.id.Row0);
    		table_row_selected.setBackgroundColor(getResources().getColor(R.color.green));
    		loadPlayers();
    		break;
    	}
    }
    
    public void row1(View v)
    {
    	switch(rowSelected){
    	case -1:
    		rowSelected = 1;
    		table_row_selected = (TableRow) findViewById(R.id.Row1);
    		table_row_selected.setBackgroundColor(getResources().getColor(R.color.blue));
    		break;
    	case 1:
    		rowSelected = -1;
    		table_row_selected = (TableRow) findViewById(R.id.Row1);
    		table_row_selected.setBackgroundColor(getResources().getColor(R.color.green));
    		break;
    	case 0:
    	case 2:
    	case 3:
    	case 4:
    		break;
    	default:
    		int tmp;
    		tmp = positions[1];
    		positions[1]=positions[rowSelected];
    		positions[rowSelected]=tmp;
    		rowSelected = -1;
    		table_row_selected.setBackgroundColor(getResources().getColor(R.color.red));
    		table_row_selected = (TableRow) findViewById(R.id.Row1);
    		table_row_selected.setBackgroundColor(getResources().getColor(R.color.green));
    		loadPlayers();
    		break;
    	}
    }
    
    public void row2(View v)
    {
    	switch(rowSelected){
    	case -1:
    		rowSelected = 2;
    		table_row_selected = (TableRow) findViewById(R.id.Row2);
    		table_row_selected.setBackgroundColor(getResources().getColor(R.color.blue));
    		break;
    	case 0:
    	case 1:
    	case 3:
    	case 4:
    		break;
    	case 2:
    		rowSelected = -1;
    		table_row_selected = (TableRow) findViewById(R.id.Row2);
    		table_row_selected.setBackgroundColor(getResources().getColor(R.color.green));
    		break;
    	default:
    		int tmp;
    		tmp = positions[2];
    		positions[2]=positions[rowSelected];
    		positions[rowSelected]=tmp;
    		rowSelected = -1;
    		table_row_selected.setBackgroundColor(getResources().getColor(R.color.red));
    		table_row_selected = (TableRow) findViewById(R.id.Row2);
    		table_row_selected.setBackgroundColor(getResources().getColor(R.color.green));
    		loadPlayers();
    		break;
    	}
    }
    
    public void row3(View v)
    {
    	switch(rowSelected){
    	case -1:
    		rowSelected = 3;
    		table_row_selected = (TableRow) findViewById(R.id.Row3);
    		table_row_selected.setBackgroundColor(getResources().getColor(R.color.blue));
    		break;
    	case 0:
    	case 1:
    	case 2:
    	case 4:
    		break;
    	case 3:
    		rowSelected = -1;
    		table_row_selected = (TableRow) findViewById(R.id.Row3);
    		table_row_selected.setBackgroundColor(getResources().getColor(R.color.green));
    		break;
    	default:
    		int tmp;
    		tmp = positions[3];
    		positions[3]=positions[rowSelected];
    		positions[rowSelected]=tmp;
    		rowSelected = -1;
    		table_row_selected.setBackgroundColor(getResources().getColor(R.color.red));
    		table_row_selected = (TableRow) findViewById(R.id.Row3);
    		table_row_selected.setBackgroundColor(getResources().getColor(R.color.green));
    		loadPlayers();
    		break;
    	}
    }
    
    public void row4(View v)
    {
    	switch(rowSelected){
    	case -1:
    		rowSelected = 4;
    		table_row_selected = (TableRow) findViewById(R.id.Row4);
    		table_row_selected.setBackgroundColor(getResources().getColor(R.color.blue));
    		break;
    	case 0:
    	case 1:
    	case 2:
    	case 3:
    		break;
    	case 4:
    		rowSelected = -1;
    		table_row_selected = (TableRow) findViewById(R.id.Row4);
    		table_row_selected.setBackgroundColor(getResources().getColor(R.color.green));
    		break;
    	default:
    		int tmp;
    		tmp = positions[4];
    		positions[4]=positions[rowSelected];
    		positions[rowSelected]=tmp;
    		rowSelected = -1;
    		table_row_selected.setBackgroundColor(getResources().getColor(R.color.red));
    		table_row_selected = (TableRow) findViewById(R.id.Row4);
    		table_row_selected.setBackgroundColor(getResources().getColor(R.color.green));
    		loadPlayers();
    		break;
    	}
    }
    
    public void row5(View v)
    {
    	switch(rowSelected){
    	case -1:
    		rowSelected = 5;
    		table_row_selected = (TableRow) findViewById(R.id.Row5);
    		table_row_selected.setBackgroundColor(getResources().getColor(R.color.blue));
    		break;
    	case 6:    		
    	case 7:
    	case 8:
    	case 9:
    		break;
    	case 5:
    		rowSelected = -1;
    		table_row_selected = (TableRow) findViewById(R.id.Row5);
    		table_row_selected.setBackgroundColor(getResources().getColor(R.color.green));
    		break;
    	default:
    		int tmp;
    		tmp = positions[5];
    		positions[5]=positions[rowSelected];
    		positions[rowSelected]=tmp;
    		rowSelected = -1;
    		table_row_selected.setBackgroundColor(getResources().getColor(R.color.green));
    		table_row_selected = (TableRow) findViewById(R.id.Row5);
    		table_row_selected.setBackgroundColor(getResources().getColor(R.color.red));
    		loadPlayers();
    		break;
    	}
    }
    
    public void row6(View v)
    {
    	switch(rowSelected){
    	case -1:
    		rowSelected = 6;
    		table_row_selected = (TableRow) findViewById(R.id.Row6);
    		table_row_selected.setBackgroundColor(getResources().getColor(R.color.blue));
    		break;
    	case 5:    		
    	case 7:
    	case 8:
    	case 9:
    		break;
    	case 6:
    		rowSelected = -1;
    		table_row_selected = (TableRow) findViewById(R.id.Row6);
    		table_row_selected.setBackgroundColor(getResources().getColor(R.color.green));
    		break;
    	default:
    		int tmp;
    		tmp = positions[6];
    		positions[6]=positions[rowSelected];
    		positions[rowSelected]=tmp;
    		rowSelected = -1;
    		table_row_selected.setBackgroundColor(getResources().getColor(R.color.green));
    		table_row_selected = (TableRow) findViewById(R.id.Row6);
    		table_row_selected.setBackgroundColor(getResources().getColor(R.color.red));
    		loadPlayers();
    		break;
    	}
    }
    
    public void row7(View v)
    {
    	switch(rowSelected){
    	case -1:
    		rowSelected = 7;
    		table_row_selected = (TableRow) findViewById(R.id.Row7);
    		table_row_selected.setBackgroundColor(getResources().getColor(R.color.blue));
    		break;
    	case 6:    		
    	case 5:
    	case 8:
    	case 9:
    		break;
    	case 7:
    		rowSelected = -1;
    		table_row_selected = (TableRow) findViewById(R.id.Row7);
    		table_row_selected.setBackgroundColor(getResources().getColor(R.color.green));
    		break;
    	default:
    		int tmp;
    		tmp = positions[7];
    		positions[7]=positions[rowSelected];
    		positions[rowSelected]=tmp;
    		rowSelected = -1;
    		table_row_selected.setBackgroundColor(getResources().getColor(R.color.green));
    		table_row_selected = (TableRow) findViewById(R.id.Row7);
    		table_row_selected.setBackgroundColor(getResources().getColor(R.color.red));
    		loadPlayers();
    		break;
    	}
    }
    
    public void row8(View v)
    {
    	switch(rowSelected){
    	case -1:
    		rowSelected = 8;
    		table_row_selected = (TableRow) findViewById(R.id.Row8);
    		table_row_selected.setBackgroundColor(getResources().getColor(R.color.blue));
    		break;
    	case 6:    		
    	case 7:
    	case 5:
    	case 9:
    		break;
    	case 8:
    		rowSelected = -1;
    		table_row_selected = (TableRow) findViewById(R.id.Row8);
    		table_row_selected.setBackgroundColor(getResources().getColor(R.color.green));
    		break;
    	default:
    		int tmp;
    		tmp = positions[8];
    		positions[8]=positions[rowSelected];
    		positions[rowSelected]=tmp;
    		rowSelected = -1;
    		table_row_selected.setBackgroundColor(getResources().getColor(R.color.green));
    		table_row_selected = (TableRow) findViewById(R.id.Row8);
    		table_row_selected.setBackgroundColor(getResources().getColor(R.color.red));
    		loadPlayers();
    		break;
    	}
    }
}
